package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.KnowledgeBaseListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.KnowledgebaseOriginal;
import com.isunland.managesystem.entity.Knowledgebasemanagement;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:  知识库list
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/2/14 10:17
 */
public class KnowledgeBaseListFragment extends BaseListFragment {
  private final int REQUEST_ADD = 0;
  private final int REQUEST_QUERY = 1;
  private final int REQUEST_DETAIL = 2;
  private ArrayList<Knowledgebasemanagement> mList;
  private KnowledgeBaseListAdapter<Knowledgebasemanagement> mAdapter;

  private String mdataStatus;
  private String mdataStatusName;

  private String mstartDate;
  private String mendDate;

  private String mdocKindCode;
  private String mdocKindName;

  private String mdocKeyWords;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = ((BaseVolleyActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {
      actionBar.setTitle(R.string.knowledgebaseList);
    }
    mList = new ArrayList<>();
    mdataStatus = "";
    mdataStatusName = getString(R.string.all);
    mstartDate = MyDateUtil.getBeforeThreeMonth();
    mendDate = MyDateUtil.parseDateToString(new Date());
    mdocKindCode = "";
    mdocKindName = "";
    mdocKeyWords = "";
  }

  //@Override
  //public void onViewCreated(View view, Bundle savedInstanceState) {
  //    super.onViewCreated(view, savedInstanceState);
  //    mListview.setDividerHeight(0);
  //}

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add_search, menu);
    menu.getItem(0).setVisible(false);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //                if (NavUtils.getParentActivityName(getActivity())!=null){
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        return true;
      //添加
      case R.id.menu_item_add:
        Intent add = new Intent(getActivity(), KnowledgeBaseDetailActivity.class);
        startActivityForResult(add, REQUEST_DETAIL);
        return true;
      //查询
      case R.id.menu_item_querys:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(mdataStatusName);
        queryConten.setExtCode1(mdataStatus);
        queryConten.setExtName2(mstartDate);
        queryConten.setExtName3(mendDate);
        queryConten.setExtName4(mdocKindName);
        queryConten.setExtCode4(mdocKindCode);
        queryConten.setExtName5(mdocKeyWords);
        Intent query = new Intent(getActivity(), KnowledgeBaseQueryActivity.class);
        query.putExtra(KnowledgeBaseQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_KNOWLEDGEBASE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    //        params.put("jobNo", CurrentUser.newInstance(getActivity()).getJobNumber());
    params.put("dataStatus", DataStatus.PUBLIS);
    params.put("startTime", mstartDate);
    params.put("endTime", mendDate);
    params.put("type", mdocKindCode);
    params.put("keyword", mdocKeyWords);
    return params;
  }

  @Override public void resolveResponse(String json) {
    KnowledgebaseOriginal result = new Gson().fromJson(json, KnowledgebaseOriginal.class);
    if (result.getResult() == Base.SUCCESS && result.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(result.getRows());
      mAdapter = new KnowledgeBaseListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    Knowledgebasemanagement content = mAdapter.getItem(position - 1);
    Intent i = new Intent(getActivity(), KnowledgeBaseDetailActivity.class);
    i.putExtra(KnowledgeBaseDetailFragment.EXTRA_CONTENT, content);
    startActivityForResult(i, REQUEST_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(KnowledgeBaseQueryFragment.EXTRA_CONTENT);
      mdataStatusName = queryConten.getExtName1();
      mdataStatus = queryConten.getExtCode1();
      mstartDate = queryConten.getExtName2();
      mendDate = queryConten.getExtName3();
      mdocKindName = queryConten.getExtName4();
      mdocKindCode = queryConten.getExtCode4();
      mdocKeyWords = queryConten.getExtName5();
    }
    if (requestCode == REQUEST_ADD) {

    }
    volleyPost();
  }
}
